package com.ningxia.water.service.impl;

import com.ningxia.water.entity.TsWellWarn;
import com.ningxia.water.entity.ZtVillageInfo;
import com.ningxia.water.mapper.TsWellWarnMapper;
import com.ningxia.water.mapper.ZtAreaInfoMapper;
import com.ningxia.water.mapper.ZtVillageInfoMapper;
import com.ningxia.water.service.ITsWellWarnService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ningxia.water.utils.LogUtils;
import com.ningxia.water.vo.out.WarnInfoVo;
import com.ningxia.water.vo.out.WellInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.ListIterator;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hz
 * @since 2021-03-15
 */
@Service
public class TsWellWarnServiceImpl extends ServiceImpl<TsWellWarnMapper, TsWellWarn> implements ITsWellWarnService {

    @Autowired
    private TsWellWarnMapper wellWarnMapper;

    @Autowired
    private ZtAreaInfoMapper areaInfoMapper;

    @Autowired
    private ZtVillageInfoMapper villageInfoMapper;

    @Override
    public Integer findByWarnCode(int warnCode) {
        return wellWarnMapper.findByWarnCode(warnCode,0);
    }

    @Override
    public Integer getWarnCountsByStatus(int status) {
        return wellWarnMapper.getWarnCountsByStatus(status);
    }

    @Override
    public Integer updateByWarnId(Integer id, Integer status) {
        return wellWarnMapper.updateByWarnId(id, status);
    }

    @Override
    public Integer getWarnCountsByWellId(Integer wellId) {
        return wellWarnMapper.getWarnCountsByWellId(wellId);
    }

    @Override
    public List<WarnInfoVo> getWarnInfos() {
        List<WarnInfoVo> warnInfos = wellWarnMapper.getWarnInfos();
        ListIterator<WarnInfoVo> it = warnInfos.listIterator();
        //根据zoneId zoneType找到对应的address
        while (it.hasNext()) {
            WarnInfoVo wiv = it.next();
            if (wiv.getZoneType() == 1) {
                wiv.setAddress(areaInfoMapper.getWellAddress(wiv.getZoneId()));
            } else {
                wiv.setAddress(villageInfoMapper.getWellAddress(wiv.getZoneId()));
            }
        }
        return warnInfos;
    }

    @Override
    public List<WarnInfoVo> findWarnByConditions(String applicantName, String wellName, Integer status, String startTime, String endTime) {
//        LogUtils.getLogInstance().info(startTime + " " + endTime);
        List<WarnInfoVo> warnInfos = wellWarnMapper.getWarnInfosByConditions(applicantName,wellName,status,startTime,endTime);

        return warnInfos;
    }
}
